import buyCarAgreement from '@/components/text-agreement/bugCar'
import bond from '@/components/text-agreement/bond'
import timLogin from '@/utils/timLogin.js'
export default {
  components: {
    buyCarAgreement,
    bond
  },
  data() {
    return {
      isBond: false, //购车保证金弹窗
      typeMenu: [{
        icon: 1,
        path: 'https://files.yzsheng.com/client/order/lianxishangjia.png',
        text: '联系商家'
      },
      {
        icon: 2,
        path: 'https://files.yzsheng.com/client/order/dianhua.png',
        text: '致电商家'
      },
      {
        icon: '/pages_order/hittiCar/hittiCar',
        path: 'https://files.yzsheng.com/client/order/lingqu1000.png',
        text: '领取1000元'
      },
      {
        icon: '/pages_commodity/doMaintenance/doMaintenance',
        path: 'https://files.yzsheng.com/client/order/yangche.png',
        text: '养车无忧'
      }
      ],
      agreeBuy: false, //用户购车协议
      isAgree: true, //false我同意选择  true未选择
      company: false, //公司方案
      show: false, //个人方案
      status: 0,
      isOpen: false, //方案说明是否打开
      content: '2020-05-21 15：16：22',
      list: [], //传过来的数据
      getList: [], //详情数据
      isShow: {}, //补交资料显示判断
      // 图片地址
      aHouseholder: '', //户主
      oneself: '', //本人
      driverLicense: '', //驾驶证正
      driverLicenseCounter: '', //驾驶证反
      certificateMarriage: '', //结婚证正
      flowingWater: '', //微信/支付宝流水
      housingContract1: '', //购房合同1
      housingContract2: '', //购房合同2
      housingContract3: '', //购房合同3
      housingContract4: '', //购房合同4
      housingContract5: '', //购房合同5
      bankFlow1: '', //银行流水1
      bankFlow2: '', //银行流水2
      flowingwatername: '未上传任何文件',
      uploadinvoice: '', //上传发票
      time: 0,
      showPage: false
    }
  },
  onLoad(op) {
    this.list = op.id
    // console.log(list)
  },
  created() {
    this.getOrderBuyCarDetailsById()
  },
  filters: {
    cutString(str, len) {
      if (str.length * 2 <= len) {
        return str
      }
      let strlen = 0
      let s = ''
      for (let i = 0; i < str.length; i++) {
        s = s + str.charAt(i)
        if (str.charCodeAt(i) > 128) {
          strlen = strlen + 2
          if (strlen >= len) {
            return s.substring(0, s.length - 1) + '...'
          }
        } else {
          strlen = strlen + 1
          if (strlen >= len) {
            return s.substring(0, s.length - 2) + '...'
          }
        }
      }
      return s
    },
    million(value) { //过万处理
      let num
      if (value > 9999) { //大于9999显示x.xx万
        num = (Math.floor(value / 1000) / 10) + '万'
      } else if (value <= 9999 && value >= -9999) {
        num = value + '元'
      } else if (value < -9999) { //小于-9999显示-x.xx万
        num = -(Math.floor(Math.abs(value) / 1000) / 10) + '万'
      }
      return num
    }
  },
  methods: {
    endTime(){
      this.time = 7200
    },
    //店铺详情
    onStore(id) {
      console.log(id)
      uni.navigateTo({
        url: '/pages_store/newCarStore/index/index?storeid=' + id
      })
    },
    returnTime(time) {
      if (time) {
        return time.split('T')[0] + ' ' + time.split('T')[1]
        // return new Date(+new Date(time) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,'')
      }
    },
    getCont() {
      this.$store.commit('news/createConversationActive', {
        user: this.getList.store_Name, //店铺名称
        img: this.getList.store_TopImg, //店铺头像
        lastTime: '', //
        userId: this.getList.store_Id, //店铺id
        conversationID: 'C2C',
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc: 'https://files.yzsheng.com/goodimg/brandimg/' + this.getList.brand_Id + '.png',
        name: this.getList.car_Type_Name + this.getList.configuration_Name,
        price: this.getList.factory_Money + '万',
        type: 'resaleCar',
        goPage: '0',
        payment: this.getList.down_Payment_Money, //首付
        monthly: this.getList.installment_Monthly_Money //月供
      })
      uni.navigateTo({
        url: '/pages_common/contactSeller/contactSeller?user=' + this.getList.store_Name + '&pageId=' +
					this.getList.id + '&tab=1'
      })
    },
    //在线咨询
    contact() {
      // 登录tim
      if (this.$store.state.isSDKReady) {
        this.$u.debounce(this.getCont(), 2000)

      } else {
        timLogin(() => {
          this.$u.debounce(this.getCont(), 2000)
        })
      }

    },
    // 拨打电话
    phone() {
      uni.makePhoneCall({
        phoneNumber: this.getList.phone
      })
    },
    goIcon(url) {
      if (url === 1) {
        this.contact()
      } else if (url === 2) {
        this.phone()
      } else {
        uni.navigateTo({
          url: url
        })
      }

    },
    goPage(item) {
      // console.log(item.order_No)
      uni.navigateTo({
        url: '/pages_order/arraignment_details/arraignment_details?id=' + item.id
      })
    },

    goPay() {
      if (this.isAgree) {
        this.agreeBuy = true
      } else {
        uni.navigateTo({
          url: '/pages_commodity/payment/payment?orderNo=' + this.getList.id + '&price=' + this
            .getList.earnest_Money + '&type=butCarOrder'
        })
      }

    },
    agreement() {
      this.isAgree = false
    },
    //勾选免责
    agree() {
      this.isAgree = !(this.isAgree)
    },
    //获取订单详情
    async getOrderBuyCarDetailsById() {
      let data = {
        id: this.list
      }
      const res = await this.$api.order.getOrderBuyCarDetailsById(data)
      this.showPage = true
      this.getList = res.data
      //计算时间
      if (res.data.closingTimePayment) {
        let a = res.data.closingTimePayment
        let b = a.split('.')[0]
        let c = b.split(':')
        let hr = c[0] * 3600
        let min = c[1] * 60
        let sec = c[2] * 1
        this.time = hr + min + sec
      }
      this.getOrderSupplementaryData()

    },
    //复制
    copyUrl() {
      uni.setClipboardData({
        data: this.getList.order_No,
        success: res => {

          uni.showToast({
            title: '复制成功',
          })
        },
        fail: err => {

          uni.showToast({
            title: '复制失败'
          })
        }
      })
    },
    //上传图片
    lastPicture(type, indexs) {

      uni.chooseImage({
        count: 1,
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        success: result => {
          uni.showLoading({
            title: '图片上传中~',
          })
          this.$api.files.uploadImg({
            filePath: result.tempFilePaths[0], // 图片流
          }).then(res => {

            var urls = res.baseUrl + res.url

            switch (type) {
            case 0:
              this.aHouseholder = urls //户主
              break
            case 1:
              this.oneself = urls //本人
              break
            case 2:
              this.driverLicense = urls //驾驶证正
              break
            case 3:
              this.driverLicenseCounter = urls //驾驶证反
              break
            case 4:
              this.certificateMarriage = urls //结婚证正
              break
            case 5:
              this.flowingWater = urls //微信/支付宝流水
              this.flowingwatername = ''
              break
            case 61:
              this.housingContract1 = urls //
              break
            case 62:
              this.housingContract2 = urls //
              break
            case 63:
              this.housingContract3 = urls //
              break
            case 64:
              this.housingContract4 = urls //
              break
            case 65:
              this.housingContract5 = urls //
              break
            case 7:
              this.bankFlow1 = urls //
              break
            case 8:
              this.bankFlow2 = urls //
              break
            case 9:
              this.uploadinvoice = urls //
              break
            }
            uni.hideLoading()
          })
        },
        error: function(e) {}
      })
    },
    getMSg() {
      uni.showToast({
        title: '等待商家交车',
        icon: 'none'
      })
    },
    //待补充资料，立即提交
    confirmSubmission() {
      if (this.aHouseholder === '' && this.isShow.residence) {
        uni.showToast({
          title: '请上传户主页',
          icon: 'none'
        })
        return false
      } else if (this.oneself === '' && this.isShow.residence) {
        uni.showToast({
          title: '请上传本人页',
          icon: 'none'
        })
        return false
      } else if (this.driverLicense === '' && this.driving_license) {
        uni.showToast({
          title: '请上传驾驶证正面',
          icon: 'none'
        })
        return false
      } else if (this.driverLicenseCounter === '' && this.driving_license) {
        uni.showToast({
          title: '请上传驾驶证副面',
          icon: 'none'
        })
        return false
      } else if (this.flowingWater === '' && this.wechat_alipay_record) {
        uni.showToast({
          title: '请上传微信/支付宝流水',
          icon: 'none'
        })
        return false
      } else if (this.bankFlow1 === '' && this.account_statement) {
        uni.showToast({
          title: '请上传银行流水',
          icon: 'none'
        })
        return false
      } else {
        this.AddOtherData()
      }
    },

    async getOrderSupplementaryData() {
      try {
        let data = {
          'order_Buy_Car_Id': this.getList.id
        }
        const res = await this.$api.order.getOrderSupplementaryData(data)
        this.isShow = res.data

      } catch (e) {
        //TODO handle the exception
      }
    },

    async AddOtherData(typeid) {
      try {
        const res = await this.$api.order.AddOtherData({
          order_Buy_Car_Id: this.getList.id,
          residence_Home_Path: this.aHouseholder,
          residence_Self_Path: this.oneself,
          driving_License_First_Path: this.driverLicense,
          driving_License_Second_Path: this.driverLicenseCounter,
          marriage_License_Path: this.certificateMarriage,
          wechat_Alipay_Record_Path: this.flowingWater,
          house_Purchase_Contract_Path: this.housingContract1,
          house_Purchase_Contract_Path2: this.housingContract2,
          house_Purchase_Contract_Path3: this.housingContract3,
          house_Purchase_Contract_Path4: this.housingContract4,
          house_Purchase_Contract_Path5: this.housingContract5,
          account_Statement_Path: this.bankFlow1,
          account_Statement_Path2: this.bankFlow2
        })
        if (res.code === 200) {
          setTimeout(function() {
            uni.showToast({
              title: '操作完成',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          uni.navigateTo({
            url: '/pages_order/buy_order/buy_order'
          })
        }

      } catch (err) {}
    },
    //代替车-确认交车
    confirmdelivery() {
      this.ConfirmGetCar()
    },
    async ConfirmGetCar(typeid) {
      try {
        const res = await this.$api.order.ConfirmGetCar({
          id: this.getList.id,
          invoiceUrl: this.uploadinvoice,
        })
        if (res.code === 200) {
          setTimeout(function() {
            uni.showToast({
              title: '确认提交完成',
              duration: 2000,
              icon: 'none'
            })
          }, 100)
          uni.navigateTo({
            url: '/pages_order/buy_order/buy_order'
          })
        }

      } catch (err) {}
    },
  }
}
